#!/bin/bash
# 安装rsync
echo "开始执行安装rsync命令"
if ! which rsync 2>/dev/null; then
  sudo apt install rsync -y
  /usr/bin/rsync --daemon --no-detach
fi
# 添加到系统启动执行命令
if [ ! `grep "rsync" /etc/rc.local  2>/dev/null` ]; then
    sed -i '$i\/usr/bin/rsync --daemon --no-detach' /etc/rc.local
fi
# 配置rsyncd文件夹
if [ ! -f "/etc/rsyncd.conf" ]; then
  sudo touch  /etc/rsyncd.conf
  echo "# 设置用户" > /etc/rsyncd.conf
  echo "uid=www-data" >> /etc/rsyncd.conf
  echo "# 设置用户组" >> /etc/rsyncd.conf
  echo "gid=www-data" >> /etc/rsyncd.conf
  echo "#禁止切换目录">> /etc/rsyncd.conf
  echo "max connections = 200">> /etc/rsyncd.conf
  echo "#设置超时时间">> /etc/rsyncd.conf
  echo "timeout = 600">> /etc/rsyncd.conf
  echo "# 设置禁止切换目录">> /etc/rsyncd.conf
  echo "use_chroot=no">> /etc/rsyncd.conf
  echo "#设置端口">> /etc/rsyncd.conf
  echo "port=873">> /etc/rsyncd.conf
  echo "# pid文件路径">> /etc/rsyncd.conf
  echo "pid file=/var/run/rsync.pid">> /etc/rsyncd.conf
  echo "#lock文件的位置">> /etc/rsyncd.conf
  echo "lock_file=/var/run/rsyncd.lock">> /etc/rsyncd.conf
  echo "# log 文件地址">> /etc/rsyncd.conf
  echo "#log file=/var/log/rsyncd/rsyncd.log">> /etc/rsyncd.conf
  echo "#添加测试模块">> /etc/rsyncd.conf
  echo "[test]">> /etc/rsyncd.conf
  echo "path=/data/web/test">> /etc/rsyncd.conf
  echo "#no代表客户端可以上传文件,yes表示只读取">> /etc/rsyncd.conf
  echo "read only=no">> /etc/rsyncd.conf
  echo "#no表示客户端可以下载文件,yes表示不能下载">> /etc/rsyncd.conf
  echo "#write_only=yes">> /etc/rsyncd.conf
  echo "hosts allow=192.168.68.132">> /etc/rsyncd.conf
  echo "list=false">> /etc/rsyncd.conf
fi